package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.elasticsearch.domain.DestinationEs;
import cn.wolfcode.wolf2w.elasticsearch.domain.StrategyEs;
import cn.wolfcode.wolf2w.elasticsearch.domain.TravelEs;
import cn.wolfcode.wolf2w.elasticsearch.domain.UserInfoEs;
import cn.wolfcode.wolf2w.elasticsearch.service.IDestinationESService;
import cn.wolfcode.wolf2w.elasticsearch.service.IStrategyESService;
import cn.wolfcode.wolf2w.elasticsearch.service.ITravelESService;
import cn.wolfcode.wolf2w.elasticsearch.service.IUserInfoESService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DataInitController {
    // es
    @Autowired
    private IStrategyESService strategyESService;
    @Autowired
    private ITravelESService travelESService;
    @Autowired
    private IUserInfoESService userInfoESService;
    @Autowired
    private IDestinationESService destinationESService;

    // mongodb
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;

    @GetMapping("/dataInit")
    public Object dataInit(){
        // 目的地
        List<Destination> destinations = destinationService.listAll();
        for (Destination destination : destinations) {
            DestinationEs destinationEs = new DestinationEs();
            BeanUtils.copyProperties(destination,destinationEs);
            destinationESService.save(destinationEs);
        }

        // 攻略
        List<Strategy> strategies = strategyService.listAll();
        for (Strategy strategy : strategies) {
            StrategyEs strategyEs = new StrategyEs();
            BeanUtils.copyProperties(strategy,strategyEs);
            strategyESService.save(strategyEs);
        }

        // 游记
        List<Travel> travels = travelService.listAll();
        for (Travel travel : travels) {
            TravelEs travelEs = new TravelEs();
            BeanUtils.copyProperties(travel,travelEs);
            travelESService.save(travelEs);
        }

        // 用户
        List<UserInfo> userInfos = userInfoService.listAll();
        for (UserInfo userInfo : userInfos) {
            UserInfoEs userInfoEs = new UserInfoEs();
            BeanUtils.copyProperties(userInfo,userInfoEs);
            userInfoESService.save(userInfoEs);
        }
        return "OK!";
    }
}
